local fk__yicha = fk.CreateSkill {

  name = "fk__yicha",

  tags = {  },

}

local U = require "packages/utility/utility"


fk__yicha:addEffect(fk.BeforeDrawCard, {

  anim_type = "control",

  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(fk__yicha.name) then return false end 
    if event == fk.BeforeDrawCard then return data.num >= 1 end
    return true
  end,
  on_cost = function(self, event, target, player, data)
    local x = player:getMark("fk__yicha-turn")
    if x ~= 0 then x = #x end
    x = 5 - x
    return player.room:askForSkillInvoke(player, fk__yicha.name, nil, "#fk__yicha-invoke:::" .. x)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:getMark("fk__yicha-turn")
    if x ~= 0 then x = #x end
    x = 5 - x
    if #room.draw_pile < x then
      room:shuffleDrawPile()
      if #room.draw_pile < x then
        room:gameOver("")
      end
    end
    local cids = table.slice(room.draw_pile, 1, x + 1)
    -- room:fillAG(player, cids)
    -- room:delay(3000)
    -- local card_arg = table.concat(table.map(cids,
    --   function(id) return Fk:getCardById(id):toLogString() end), ",")
    local to_discard = room:askForCardsChosen(player, player, 0, #cids, {
      card_data = {
        { "Top", cids }
      }
    }, fk__yicha.name, "#fk__yicha-discard:::" .. x)
    room:moveCardTo(to_discard, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, fk__yicha.name)
  end,
})

fk__yicha:addEffect(fk.StartJudge, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(fk__yicha.name) then return false end 
    if event == fk.BeforeDrawCard then return data.num >= 1 end
    return true
  end,
  on_cost = function(self, event, target, player, data)
    local x = player:getMark("fk__yicha-turn")
    if x ~= 0 then x = #x end
    x = 5 - x
    return player.room:askForSkillInvoke(player, fk__yicha.name, nil, "#fk__yicha-invoke:::" .. x)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:getMark("fk__yicha-turn")
    if x ~= 0 then x = #x end
    x = 5 - x
    if #room.draw_pile < x then
      room:shuffleDrawPile()
      if #room.draw_pile < x then
        room:gameOver("")
      end
    end
    local cids = table.slice(room.draw_pile, 1, x + 1)
    -- room:fillAG(player, cids)
    -- room:delay(3000)
    -- local card_arg = table.concat(table.map(cids,
    --   function(id) return Fk:getCardById(id):toLogString() end), ",")
    local to_discard = room:askForCardsChosen(player, player, 0, #cids, {
      card_data = {
        { "Top", cids }
      }
    }, fk__yicha.name, "#fk__yicha-discard:::" .. x)
    room:moveCardTo(to_discard, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, fk__yicha.name)
  end,
})
fk__yicha:addEffect(fk.AfterCardsMove, {
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(yicha.name, true) then
      local room = player.room
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          local tmp = table.simpleClone(player:getMark("fk__yicha-turn") ~= 0 and player:getMark("fk__yicha-turn") or {})
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(tmp, Fk:getCardById(info.cardId):getSuitString(true))
          end
          room:setPlayerMark(player, "fk__yicha-turn", tmp)
          return player:getMark("fk__yicha-turn") ~= 0
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@fk__yicha-turn", table.concat(table.map(player:getMark("fk__yicha-turn"), Util.TranslateMapper)))
  end
})

return fk__yicha